package com.tdly.buduizhong.base;

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class BaseActivity extends AppCompatActivity{

    //传感器 ,这里默认同时需要和三个硬件连接，分别设置id 1,2,3进行区分，demo中实际只用到 MAGIKARE_SENSOR_DOWN = 1
    //可以根据情况自行添加删除
    public static final int MAGIKARE_SENSOR_UP = 2;
    public static final int MAGIKARE_SENSOR_DOWN = 1;
    public static final int MAGIKARE_SENSOR_CENTER = 3;

    public static float[] m_receive_data_up;                    //传感器的数据
    public static float[] m_receive_data_down;                  //传感器的数据 ,demo中我们只需要这一个，因为只有一个硬件设备，
    public static float[] m_receive_data_center;                //传感器的数据
    //hanlder消息标识 message.what
    public static final int MESSAGE_STATE_CHANGE = 1; // 状态改变
    public static final int MESSAGE_READ = 2;          // 读取数据
    public static final int MESSAGE_WRITE = 3;         // 给硬件传数据，暂不需要，看具体需求
    public static final int MESSAGE_DEVICE_NAME = 4;  // 设备名字
    public static final int MESSAGE_TOAST = 5;         // Toast

    public static final byte[][] datastr={
            {0x01,(byte)0x80,0x11,0x00,0x00,(byte)0x92},//加速度值
            {0x01,(byte)0x80,0x21,0x00,0x00,(byte)0xA2},//速度值
            {0x01,(byte)0x80,0x31,0x00,0x00,(byte)0xB2},//位移值
            {0x01,(byte)0x80,0x14,0x00,0x08,0x00,(byte)0x96}//加速度波形
    };






    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);

    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }

    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }

    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    public static String[] byteToString(String tmp){
        String[] data=tmp.replace("[","").replace("]","").trim().split(",");
        return  data;
    }

    public static int StrToInt(String str) {
        if(str==null || str.length() == 0){
            return 0;
        }
        int n = str.length(), s = 1;
        char[] chs = str.toCharArray();
        int res = 0;
        if(chs[0] == '-') s = -1;
        for(int i = (chs[0] ==  '-' || chs[0] == '+') ? 1 : 0; i < n; ++i){
            if(!('0' <= chs[i] && chs[i] <= '9')) return 0;
            res = (res << 1) + (res << 3) + (chs[i] & 0xf);//res=res*10+str[i]-'0';
        }
        return (int)res * s;
    }
}
